package com.heyue.demo.thread;
/**
 * @author fenghe
 * @date 2022/2/13 11:09
 */
public class DeadThread extends Thread {
    static Object o1 = new Object();
    static Object o2 = new Object();
    boolean flag;

    public DeadThread(boolean flag) {
        this.flag = flag;
    }
    @Override
    public void run() {
        if(flag) {
            synchronized (o1) {
                System.out.println(Thread.currentThread().getName() + "进入了1");
                synchronized (o2) {
                    System.out.println(Thread.currentThread().getName() + "进入了2");
                }
            }
        } else {
            synchronized (o2) {
                System.out.println(Thread.currentThread().getName() + "进入了3");
                synchronized (o1) {
                    System.out.println(Thread.currentThread().getName() + "进入了4");
                }
            }
        }
    }

    public static void main(String[] args) {
        DeadThread A = new DeadThread(true);
        DeadThread B = new DeadThread(false);
        A.start();
        B.start();
    }
}

